package com.smile.interceptor;


import cn.hutool.json.JSONUtil;
import com.smile.annotation.ExcelExport;
import com.smile.entity.common.CommonTaskEntity;
import com.smile.enums.ExcelBizTypeEnum;
import com.smile.enums.TaskStatusEnum;
import com.smile.enums.TaskTypeEnum;
import com.smile.mapper.common.CommonTaskMapper;
import com.smile.util.FillUserUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
* 文件导出拦截器
*
* @description:
  * @author: tanjialong
  * @date: 2025/12/4 16:42

**/

@Aspect
@Component
public class CommonTaskAspect {
    @Autowired
    private CommonTaskMapper commonTaskMapper;

    @Pointcut("@annotation(com.smile.annotation.ExcelExport)")
    public void pointcut() {
    }

    @Before("pointcut()")
    public void before(JoinPoint joinPoint) throws Exception {

        //获取拦截的方法名称
        String targetName = joinPoint.getTarget().getClass().getName();

        Class targetClass = Class.forName(targetName);

        //获取切入的方法名称
        String methodName = joinPoint.getSignature().getName();

        // 获取切入方法参数
        Object[] arguments = joinPoint.getArgs();
        // 获取目标类的所有方法
        Method[] methods = targetClass.getMethods();

        for (Method method : methods) {
            // 方法名相同、包含目标注解、方法参数个数相同（避免有重载）
            if(method.getName().equals(methodName) && method.isAnnotationPresent(ExcelExport.class)
            && method.getParameterTypes().length == arguments.length){
                //判断是哪个类型的导出
                ExcelBizTypeEnum excelBizTypeEnum = method.getAnnotation(ExcelExport.class).value();

                //填充参数
                CommonTaskEntity commonTaskEntity = createCommonTaskEntity(excelBizTypeEnum);

                if(ArrayUtils.isNotEmpty(arguments)){
                    Object param = arguments[0];
                    commonTaskEntity.setRequestParam(JSONUtil.toJsonStr(param));
                }
                //记录导出任务
                commonTaskMapper.insert(commonTaskEntity);
            }
        }

    }

    private CommonTaskEntity createCommonTaskEntity(ExcelBizTypeEnum excelBizTypeEnum) {
        CommonTaskEntity commonTaskEntity = new CommonTaskEntity();
        commonTaskEntity.setName(getTaskName(excelBizTypeEnum));
        commonTaskEntity.setStatus(TaskStatusEnum.WAITING.getValue());
        commonTaskEntity.setFailureCount(0);
        commonTaskEntity.setType(TaskTypeEnum.MENU.getValue());
        commonTaskEntity.setBizType(excelBizTypeEnum.getValue());
        FillUserUtil.fillCreateUserInfo(commonTaskEntity);
        return commonTaskEntity;
    }

    private String getTaskName(ExcelBizTypeEnum excelBizTypeEnum) {
        return String.format("导出%s数据", excelBizTypeEnum.getDesc());
    }

}
